<script type="text/javascript">
    function sendErrorMessages() {
        var contractId = $("#contractId").val();
        var companyId = $("#companyId").val();
        var str = "";
        $.each($('input:checkbox:checked'),function(){
            str += $(this).val() + ",";
        });
        if ($('input:checkbox:checked').length > 0) {
            if (confirm("确定审核不通过吗？")){
                $.post("<@spring.url "/manage/companyInfo/unValidate"/>",{
                    companyId:companyId, contractId:contractId, str:str}, function (data) {
                    alert(data.message);
                });
            }
        } else {
            alert("请勾选错误信息。");
        }
    }

    function messagePass() {
        var contractId = $("#contractId").val();
        var companyId = $("#companyId").val();
        if (confirm("确定审核通过吗？")){
            $.post("<@spring.url "/manage/companyInfo/validate"/>",{
                companyId:companyId, contractId:contractId}, function (data) {
                alert(data.message);
            });
        }
    }
    
    function checkInfo() {
        $("#totalError").hide();
        $("#name_err").hide();
        $("#creditCode_err").hide();
        $("#operName_err").hide();
        $("#address_err").hide();
        $("#elecFor_err").hide();
        $("#contractName_err").hide();
        $("#contractPhone_err").hide();
        $("#wxNum_err").hide();
        $("#voltageStep_err").hide();
        $("#elecAccount_error").hide();
        $("#transformerCapacity_err").hide();
        $("#elecConsumption_err").hide();
        $("#detailCategory_err").hide();
        $("#bankName_err").hide();
        $("#bankAccountName_err").hide();
        $("#bankAccount_err").hide();
        $("#contactPhone_err").hide();
        $("#fax_err").hide();
        $("#customerRate_err").hide();
        $("#bottomPrice_err").hide();
        $("#description_err").hide();

        var companyName = $.trim($("#companyName").val());
        if (companyName == "") {
            $("#name_err").show();
            return false;
        }
        var creditCode = $.trim($("#creditCode").val());
        if (creditCode == "") {
            $("#creditCode_err").show();
            return false;
        }
        var operName = $.trim($("#operName").val());
        if (operName == "") {
            $("#operName_err").show();
            return false;
        }
        var address = $.trim($("#address").val());
        if (address == "") {
            $("#address_err").show();
            return false;
        }
        var elecFor = $.trim($("#elecFor").val());
        var cityId = $("#cityId").val();
        var voltageStep = $.trim($("#voltageStep").val());
        if (voltageStep == "") {
            $("#voltageStep_err").show();
            return false;
        }
        var elecConsumption = $.trim($("#elecConsumption").val());
        if (elecConsumption == "" || isNaN(elecConsumption)) {
            $("#elecConsumption_err").show();
            return false;
        }


        var accounts = $.trim($("#elecAccount").val());
        /*if (accounts == "") {
            $("#elecAccount_error").show();
            return false;
        }*/
        var transformerCapacity = $.trim($("#transformerCapacity").val());
        if (transformerCapacity != "" && isNaN(transformerCapacity)) {
            $("#transformerCapacity_err").show();
            return false;
        }
        var contractName = $.trim($("#contractName").val());
        /*if (contractName == "") {
            $("#contractName_err").show();
            return false;
        }*/
        var contractPhone = $.trim($("#contractPhone").val());
        /*if (contractPhone == "") {
            $("#contractPhone_err").show();
            return false;
        }*/
        var wxNum = $.trim($("#wxNum").val());
        var capital = $("#capital").val();
        var detailCategory = $.trim($("#detailCategory").val());
        /*if (detailCategory == "") {
            $("#detailCategory_err").show();
            return false;
        }*/
        var bankName = $.trim($("#bankName").val());
        /*if (bankName == "") {
            $("#bankName_err").show();
            return false;
        }*/
        var bankAccountName = $.trim($("#bankAccountName").val());
        /*if (bankAccountName == "") {
            $("#bankAccountName_err").show();
            return false;
        }*/
        var bankAccount = $.trim($("#bankAccount").val());
        //if (bankAccount != "" && (isNaN(bankAccount) || bankAccount.length>22 || bankAccount.length<16)) {
        //    $("#bankAccount_err").show();
        //    return false;
        //}
        var contactPhone = $.trim($("#contactPhone").val());
        var reg=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
        if(contactPhone != "" && !reg.test(contactPhone)) {
            $("#contactPhone_err").show();
            return false;
        }

        var settingType = $("input:radio[name='settingType']:checked").val();
        if (settingType == "3") {
            var companyRate = $.trim($("#companyRate").val());
            if (companyRate == "" || isNaN(companyRate) || companyRate<0 || companyRate>100) {
                $("#customerRate_err").show();
                return false;
            }
        } else if (settingType == "4") {
            var bottomPrice = $.trim($("#bottomPrice").val());
            if (bottomPrice == "") {
                $("#bottomPrice_err").show();
                return false;
            }
        } else {
            var description = $.trim($("#description").val());
            if (description == "") {
                $("#description_err").show();
                return false;
            }
        }
        var tradeCycle = $.trim($("#tradeCycle").val());
        if (tradeCycle == "" || isNaN(tradeCycle)) {
            $("#tradeCycle_err").show();
            return false;
        }
        /*var startAt = $.trim($("#startAt").val());
        if (startAt == "") {
            $("#startAt_err").show();
            return false;
        }
        var endedAt = $("#endedAt").val();
        if (endedAt == "") {
            $("#endedAt_err").show();
            return false;
        }
        if (endedAt<=startAt) {
            $("#endedAt_err").show();
            return false;
        }*/
        return true;
    }

    function createInfo() {
        if(checkInfo()) {
            var companyName = $("#companyName").val();
            var creditCode = $("#creditCode").val();
            var operName = $("#operName").val();
            var address = $("#address").val();
            var elecFor = $("#elecFor").val();
            var contractName = $("#contractName").val();
            var contractPhone = $("#contractPhone").val();
            var wxNum = $("#wxNum").val();
            var cityId = $("#cityId").val();
            var capital = $("#capital").val();
            var voltageStep = $("#voltageStep").val();
            var accountNum = $("#accountNum").val();
            var elecAccount = $("#elecAccount").val();
            var transformerCapacity = $("#transformerCapacity").val();
            var elecConsumption = $("#elecConsumption").val();
            var detailCategory = $("#detailCategory").val();
            var bankName = $("#bankName").val();
            var bankAccountName = $("#bankAccountName").val();
            var bankAccount = $("#bankAccount").val();
            var contactPhone = $("#contactPhone").val();
            var fax = $("#fax").val();
            var tradeCycle = $("#tradeCycle").val();
            //var startAt = $("#startAt").val();
            //var endedAt = $("#endedAt").val();
            var settingType = $("input:radio[name='settingType']:checked").val();
            var customerRate = $.trim($("#customerRate").val());
            var bottomPrice = $.trim($("#bottomPrice").val());
            var description = $("#description").val();
            var companyBakId = $("#companyBakId").val();
            $.post("<@spring.url "/sale/companyInfo/create"/>",{
                companyName:companyName,creditCode:creditCode, operName:operName, address:address, elecFor:elecFor, contractName:contractName,
                contractPhone:contractPhone, wxNum:wxNum, cityId:cityId, capital:capital, voltageStep:voltageStep,
                accountNum:accountNum, elecAccount:elecAccount, transformerCapacity:transformerCapacity,
                elecConsumption:elecConsumption, detailCategory:detailCategory, bankName:bankName,
                bankAccountName:bankAccountName, bankAccount:bankAccount, contactPhone:contactPhone,
                fax:fax, tradeCycle:tradeCycle, startAt:null, endedAt:null, description:description,
                settingType:settingType, customerRate:customerRate, bottomPrice:bottomPrice, companyBakId:companyBakId}, function (data) {
                alert(data.message);
                if (data.code == 1) {
                    window.location.href="/sale/companyInfo/settlementList";
                }
            });
        } else {
            $("#totalError").show();
        }
    }

    function editInfo() {
        if(checkInfo()) {
            var companyName = $("#companyName").val();
            var creditCode = $("#creditCode").val();
            var operName = $("#operName").val();
            var address = $("#address").val();
            var elecFor = $("#elecFor").val();
            var contractName = $("#contractName").val();
            var contractPhone = $("#contractPhone").val();
            var wxNum = $("#wxNum").val();
            var cityId = $("#cityId").val();
            var capital = $("#capital").val();
            var voltageStep = $("#voltageStep").val();
            var accountNum = $("#accountNum").val();
            var elecAccount = $("#elecAccount").val();
            var transformerCapacity = $("#transformerCapacity").val();
            var elecConsumption = $("#elecConsumption").val();
            var detailCategory = $("#detailCategory").val();
            var bankName = $("#bankName").val();
            var bankAccountName = $("#bankAccountName").val();
            var bankAccount = $("#bankAccount").val();
            var contactPhone = $("#contactPhone").val();
            var fax = $("#fax").val();
            var tradeCycle = $("#tradeCycle").val();
            //var startAt = $("#startAt").val();
            //var endedAt = $("#endedAt").val();
            var settingType = $("input:radio[name='settingType']:checked").val();
            var customerRate = $.trim($("#customerRate").val());
            var bottomPrice = $.trim($("#bottomPrice").val());
            var description = $("#description").val();
            var contractId = $("#contractId").val();
            var companyId = $("#companyId").val();
            var detailId = $("#detailId").val();
            $.post("<@spring.url "/sale/companyInfo/update"/>",{
                companyId:companyId, contractId:contractId, detailId:detailId,
                companyName:companyName,creditCode:creditCode, operName:operName, address:address, elecFor:elecFor, contractName:contractName,
                contractPhone:contractPhone, wxNum:wxNum, cityId:cityId, capital:capital, voltageStep:voltageStep,
                accountNum:accountNum, elecAccount:elecAccount, transformerCapacity:transformerCapacity,
                elecConsumption:elecConsumption, detailCategory:detailCategory, bankName:bankName,
                bankAccountName:bankAccountName, bankAccount:bankAccount, contactPhone:contactPhone,
                fax:fax, tradeCycle:tradeCycle, startAt:null, endedAt:null, description:description,
                settingType:settingType, customerRate:customerRate, bottomPrice:bottomPrice}, function (data) {
                alert(data.message);
                if (data.code == 1) {
                    window.location.href="/sale/companyInfo/settlementList";
                }
            });
        } else {
            $("#totalError").show();
        }
    }

    function changeCustomerRate() {
        var companyRate = $.trim($("#companyRate").val());
        if (companyRate != null && !isNaN(companyRate) && companyRate>0 && companyRate<100) {
            $("#customerRate").val(100-parseFloat(companyRate));
        }
    }

    function chooseArea() {
        var cityId = $("#parentId").val();
        $.post("<@spring.url "/common/chooseArea"/>",{
            cityId:cityId}, function (data) {
                var str = "";
                if (data.result != null && data.result.length>0) {
                    for(var i=0; i<data.result.length; i++) {
                        str += "<option value='"+data.result[i].id+"'>"+data.result[i].name+"</option>";
                    }
                }
                $("#cityId").empty();
                $("#cityId").append(str);
                $("#cityId").selectpicker('refresh');
        });
    }

    function searchCompany() {
        var searchText = $("#companyName").val();
        $.post("<@spring.url "/sale/companyInfo/searchCompany"/>",{
            companyName:searchText}, function (data) {
            $("#newPanel").html(data);
            $("#parentId").selectpicker('refresh');
            $("#cityId").selectpicker('refresh');
            $("#voltageStep").selectpicker('refresh');
            $("#detailCategory").selectpicker('refresh');
        });
    }

</script>